quantmod


Quantitative Financial Modelling & Trading Framework for R
{ examples :: data }

Working with time-based data:

  1. OHLC data - the basics
  2. OHLC - beyond the obvious
  3. Subsetting by date - characters and '::' notation
  4. The last 3 days of the first 2 weeks - interested yet?
  5. Minutes to hours to days to months - fast aggregation.
  6. Apply by Period - weekly, monthly or arbitrarily

The OHLC Basics

The basic functions make life easier. Assuming some standard naming conventions quantmod makes available 3 primary types of column extraction functions. If you're looking for the open, the high, or the series low - it's available:
  • Op,Hi,Lo,Cl,Vo,Ad - do pretty much what they say - extract the columns Open, High, Low, Close, Volume, and Adjusted (Yahoo)
  • is.OHLC, has.OHLC, has.Op,has.Cl,has.Hi,has.Lo,has.Ad, and has.Vo - fairly obvious
  • seriesHi and seriesLo
A few examples:
> getSymbols("GS") #Goldman OHLC from yahoo
[1] "GS"
> is.OHLC(GS) # does the data contain at least OHL and C?
> has.Vo(GS) # how about volume?
> Op(GS) # just the Open column please.
> seriesHi(GS) # where and what was the high point


> OpCl(GS) #daily percent change open to close
> OpOp(GS) #one period open to open change
> HiCl(GS) #the percent change from high to close

  • Lag: What was the previous value in the series
  • Next: What is the next value in the series
  • Delt: Compute the change (delta) from two prices

> Lag(Cl(GS)) #One period lag of the close
> Lag(Cl(GS),c(1,3,5)) #One, three, and five period lags
> Next(OpCl(GS)) #The next periods open to close - today!
>
> # Open to close one-day, two-day and three-day lags
> Delt(Op(GS),Cl(GS),k=1:3)

The documentation contain all details.

Subsetting by Time and Date? -- xts Makes It Easy

The xts package made it easy to work with time-based series.


> GS['2007'] #returns all Goldman's 2007 OHLC
> GS['2008'] #now just 2008
> GS['2008-01'] #now just January of 2008
> GS['2007-06::2008-01-12'] #Jun of 07 through Jan 12 of 08
>
> GS['::'] # everything in GS
> GS['2008::'] # everything in GS, from 2008 onward
> non.contiguous <- c('2007-01','2007-02','2007-12')
> GS[non.contiguous]

The general format for the above is CCYY-MM-DD HH:MM:SS, with ranges specified via the '::' operator. Specify from left to right - that is to get January, you need to specify the year first.

The Last 3 Days of The First 2 Weeks

> last(GS) #returns the last obs.
> last(GS,8) #returns the last 8 obs.
>
> # let's try something a bit cooler.
> last(GS, '3 weeks')
> last(GS, '-3 weeks') # all except the last 3 weeks
> last(GS, '3 months')
> last(first(GS, '2 weeks'), '3 days')

Aggregating to a different time scale

to.weekly or to.monthly convert it to weekly or monthly OHLC data.

Minute data can become 5 or 10 minute data (to.minutes5 and to.minutes10, respectively).

Is your data weekly, daily, or hourly? A call to periodicity will provide the answer; a call to nweeks will tell you the number of weeks as well.

> periodicity(GS)
> unclass(periodicity(GS))
> to.weekly(GS)
> to.monthly(GS)
> periodicity(to.monthly(GS))
> ndays(GS); nweeks(GS); nyears(GS)
>
> # Let's try some non-OHLC to start
> getFX("USD/EUR")
[1] "USDEUR"
> periodicity(USDEUR)
> to.weekly(USDEUR)
> periodicity(to.weekly(USDEUR))

Apply by Period

To identify endpoints in your data by date with the function endpoints.
Use those endpoints with the functions in the period.apply family.
Quickly calculate periodic minimums, maximums, sums, and products - as well as general applys (with the periodic slant) with a few simple functions.

> endpoints(GS,on="months")
>
> # find the maximum closing price each week
> apply.weekly(GS,FUN=function(x) { max(Cl(x)) } )
>
> # the same thing - only more general
> period.apply(GS,endpoints(GS,on='weeks'),
+ FUN=function(x) { max(Cl(x)) } )
>
> # same thing - only 50x faster!
> as.numeric(period.max(Cl(GS),endpoints(GS,on='weeks')))

Period Returns

To calculate returns over calendar periods - the function periodReturn.
The indexAt argument underlying to.period versions to.monthly and to.quarterly.
indexAt set the resulting index to the first of each period (firstof), the last of each period (lastof), the starting observation of the period (startof), the ending observation of the period (endof), the month of the period (yearmon) or the quarter of the period (yearqtr).
For most classes of time-series data this defaults to yearmon for monthly observations and yearqtr for quarterly requests.
> # Quick returns - quantmod style
>
> getSymbols("SBUX")
[1] "SBUX"
> dailyReturn(SBUX) # returns by day
> weeklyReturn(SBUX) # returns by week
> monthlyReturn(SBUX) # returns by month, indexed by yearmon
>
> # daily,weekly,monthly,quarterly, and yearly
> allReturns(SBUX) # note the plural